﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using Utility;

namespace ConsoleTest.cs {
    class Program {
        static void Main(string[] args) {
            try {
                //DirectoryInfo dir = new DirectoryInfo(@"E:\My Box Files\SyncRoot\imgs");

                //FileInfo[] files = dir.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);

                //foreach (var file in files) {
                //    string name = file.Name.Substring(0, file.Name.Length - 4);
                //    BinryAndHist(file.FullName, name);
                //}

                using (Bitmap bmp = (Bitmap)Bitmap.FromFile(@"E:\My Box Files\SyncRoot\imgs\p_93.jpg")) {
                    int[,] data;
                    int ylen = 2;
                    int xlen = 8;
                    ImageHelper.GetDataRange(bmp, 0, xlen, 18, ylen, out data);

                    using (StreamWriter writer = File.AppendText("file.txt")) {
                        for (int y = 0; y < ylen; y++) {
                            for (int x = 0; x < xlen; x++) {
                                byte[] bytes = BitConverter.GetBytes(data[y, x]);
                                writer.Write("{0}-{1}-{2} ", bytes[0], bytes[1], bytes[2]);
                            }
                            writer.WriteLine();
                        }
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }
        }

        private static void BinryAndHist(string fullname, string filename) {
            filename = "imgs\\" + filename;
            string binaryFile = filename + "_bin.jpg";
            string histFile = filename + "_hist.jpg";

            using (Bitmap src = (Bitmap)Image.FromFile(fullname)) {
                using (var gray = ImageHelper.Grayscale(src)) {
                    int threshold = ImageHelper.CalculateThreshold(gray);
                    using (var binary = ImageHelper.Binarization(gray, (byte)threshold)) {
                        binary.Save(binaryFile, ImageFormat.Jpeg);
                        int[] hist;
                        ImageHelper.GetHistogram(gray, (byte)threshold,
                            out hist);
                        ImageHelper.DrawHistogram(hist, 20, histFile);
                    }
                }
            }
        }

        private static void GrayAndBiniary(string filename) {
            string file = filename + ".jpg";
            string grayFile = filename + "_gray.jpg";
            string binaryFile = filename + "_bin.jpg";
            using (Bitmap bmp = (Bitmap)Bitmap.FromFile(file)) {
                using (Bitmap gray = ImageHelper.Grayscale(bmp)) {

                    gray.Save(grayFile, ImageFormat.Jpeg);

                    int threshold = ImageHelper.CalculateThreshold(gray);
                    using (Bitmap binaryImage = ImageHelper.Binarization(gray, (byte)threshold)) {
                        binaryImage.Save(binaryFile, ImageFormat.Jpeg);
                    }
                }
            }
        }
    }
}
